package billing.i.sys;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Ibatis {
	private static final Ibatis intance = new Ibatis();
	private final SqlSessionFactory factory;

	public Ibatis() {
		String resource = "resources/configuration.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 创建SqlSessionFactory实例。没有指定要用到的
		// environment，则使用默认的environment
		factory = new SqlSessionFactoryBuilder().build(reader);
	}

	public static SqlSession getSession() {
		return intance.openSession();
	}

	private SqlSession openSession() {
		return factory.openSession();
	}
}
